using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Moussavi
{
    public partial class Form1 : Form
    {
        //_________________________________________________________________
        private Bitmap m_bmImage;
        private SerialCommManager m_SerialPort;
        private int m_HorizRes; // Default 64
        private int m_ColDepth; // For now is fixed to 4
        private int m_LEDType;  // 0 - RGB; 1 - UNICOLOR

        private double m_HWVersion;
        private int m_HWMemorySize;
        private int m_HWCPUSpeed;
        private Bitmap m_ConvImage;
        private ImageConverter m_Converter;

        //_________________________________________________________________
        public Form1()
        {
            InitializeComponent();
            m_HorizRes = 64;
            m_ColDepth = 2;
            m_LEDType = 1; // unicolor as default

            m_SerialPort = new SerialCommManager("MOUSSAVI");

            try
            {
                m_SerialPort.OpenPort();
            }
            catch
            {
                MessageBox.Show("Couldn't find the device Moussavi!");
            }

            // The serial port is open?
            if (m_SerialPort.IsOpen())
            {
                // Get properties
                String sProperties = m_SerialPort.ReadDeviceProperties();
                int iVersionPos = sProperties.IndexOf("VER:") + "VER:".Length;
                int iMemSizePos = sProperties.IndexOf("MEMORY SIZE:") + "MEMORY SIZE:".Length;
                int iCPUSpeedPos = sProperties.IndexOf("CPU SPEED:") + "CPU SPEED:".Length;
                int iNumOfLEDsPos = sProperties.IndexOf("NUMBER OF LEDS:") + "NUMBER OF LEDS:".Length;
                int iRGBLEDs = sProperties.IndexOf("RGB LEDS:") + "RGB LEDS:".Length;

                m_HWVersion = double.Parse(sProperties.Substring(iVersionPos, 6));
                m_HWMemorySize = int.Parse(sProperties.Substring(iMemSizePos, 5));
                m_HWCPUSpeed = int.Parse(sProperties.Substring(iCPUSpeedPos, 3));
                m_HorizRes = int.Parse(sProperties.Substring(iNumOfLEDsPos, 3));

                if (int.Parse(sProperties.Substring(iRGBLEDs, 1)) == 1)
                {
                    m_LEDType = 0;
                    m_LEDsType.Text = "RGB LEDs";
                }
                else
                {
                    m_LEDType = 1;
                    m_LEDsType.Text = "UniColor LEDs";
                }

                this.Text = "Moussavi Image Loader [Connected To Version " + m_HWVersion.ToString() + "]";
                m_HorizontalRes.Value = m_HorizRes;
            }
            else
            {
                this.Text = "Moussavi Image Loader [Disconnected]";
            }
        }

        private void m_LoadImageButton_Click(object sender, EventArgs e)
        {
            if (DialogResult.Cancel != m_OpenFileDialog.ShowDialog())
            {
                m_bmImage = new Bitmap(m_OpenFileDialog.FileName);
                m_Picture.Image = m_bmImage;

                m_Converter = new ImageConverter(m_bmImage, m_HorizRes, m_LEDType, m_ColDepth);

                m_ConvertedImage.Image = m_Converter.ConvertedImage;
                m_ConvImage = m_Converter.ConvertedImage;

                m_ResultImageSize.Value = m_ConvImage.Width * m_ConvImage.Height * (int)(Math.Log((double)(m_ColDepth), 2.0));
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_SerialPort.Dispose();
        }

        private void m_HorizontalRes_ValueChanged(object sender, EventArgs e)
        {
            m_HorizRes = int.Parse(m_HorizontalRes.Value.ToString());
        }

        private void m_ColorDepth_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (m_ColorDepth.Text)
            {
                case "2":
                    m_ColDepth = 2;
                    break;
                case "4": 
                    m_ColDepth = 4;
                    break;
                case "16": 
                    m_ColDepth = 16;
                    break;
            }

            ImageConverter Converter = new ImageConverter(m_bmImage, m_HorizRes, m_LEDType, m_ColDepth);

            m_ConvertedImage.Image = Converter.ConvertedImage;
            m_ConvImage = Converter.ConvertedImage;

            m_ResultImageSize.Value = m_ConvImage.Width * m_ConvImage.Height * (int)(Math.Log((double)(m_ColDepth), 2.0));
        }

        private void m_LEDsType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (m_LEDsType.Text)
            {
                case "RGB LEDs":
                    m_LEDType = 0;
                    break;
                case "UniColor LEDs":
                    m_LEDType = 1;
                    break;
           }

        }

        private void m_UploadImageButton_Click(object sender, EventArgs e)
        {
            if (m_ConvImage == null) return;

            m_SerialPort.WritePictureContents(m_ConvImage.Width, m_ConvImage.Height, m_ColDepth, (m_LEDType == 0) ? 1 : 0, m_Converter.ConvertedBuffer, m_UploadProgress);
        }

        private void button1_Click(object sender, EventArgs e)
        {
           // m_SerialPort.ReadMemoryContents(
        }
    }
}